var express = require("express");
var router = express.Router();
const {
  checkEditWxServerConfigRequest,
  sign,
  respondText,
  createtag,
  batchtagging,
  createMenu,
  gettags,
  respondArticles,
  respondPic,
  previewToUser,
  getAllUserOpenid,
  uploadTemporaryMedia,
  addLimitMaterial,
  messageToAlluser,
} = require("../app/index");

/* GET home page. */
router.get("/", function (req, res, next) {
  res.render("index", { title: "Express" });
});

router.get("/wx", async function (req, res) {
  const { signature, timestamp, nonce, echostr } = req.query;
  if (checkEditWxServerConfigRequest(signature, timestamp, nonce)) {
    res.set("Content-Type", "text/plain");
    res.send(echostr);
  } else {
    res.send("Error!!!!");
  }
});

router.post("/wx", async function (req, res) {
  const { signature, timestamp, nonce, openid } = req.query;
  console.log('"/wx"', req.body);
  const message = req.body.xml;
  if (checkEditWxServerConfigRequest(signature, timestamp, nonce)) {
    // res.set('Content-Type', 'text/plain')
    res.send(
      respondPic(
        openid,
        "sL-w-mPmhOYBPw-vgxgzQbfoOm-_2L385EIevAb0by2uqdQxyPW2amVEA6evO4sh"
      )
    );
    // res.send(
    //   respondArticles(openid, [
    //     {
    //       title: "测试",
    //       description: "测试描述",
    //       picurl:
    //         "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3958252857,3725534399&fm=26&gp=0.jpg",
    //       url: "http://frp.familybaby.top",
    //     },
    //     {
    //       title: "测试1",
    //       description: "测试描述1",
    //       picurl:
    //         "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3958252857,3725534399&fm=26&gp=0.jpg",
    //       url: "http://frp.familybaby.top",
    //     },
    //     {
    //       title: "测试2",
    //       description: "测试描述2",
    //       picurl:
    //         "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3958252857,3725534399&fm=26&gp=0.jpg",
    //       url: "http://frp.familybaby.top",
    //     },
    //   ])
    // );
  } else {
    res.send("Error!!!!");
  }
});

router.get("/createtag", async function (req, res) {
  // const id = await createtag("测试");
  // const id = { tag: { id: 100, name: "测试" } };
  // console.log(id);
  // const batch = await batchtagging(
  //   ["odkSJ58D9ISd7y1YVxcFt8fbywKY", "odkSJ5-zNfHuaNBfTSN_bii4nFH4"],
  //   100
  // );
  // const ress = await gettags();
  // const data = getAllUserOpenid();
  // const rsd = await messageToAlluser(100, "test");
  const rsd = await previewToUser("odkSJ5-zNfHuaNBfTSN_bii4nFH4", "test");
  // const ress = await uploadTemporaryMedia(
  //   "image",
  //   "C:\\Users\\fky\\Desktop\\新建文件夹\\1599203305(1).jpg"
  // );
  res.send([rsd]);
});
router.get("/addMaterial", async function (req, res) {
  const rsdm = await addLimitMaterial();
  console.log(rsdm);
  //B98ZgPOaPcqJnX2Vsae19GOTYS9yCpxQn702QYYUGtyHdJd_7-9dg1DzAfB2maqx 图文素材
  // const rsd = await messageToAlluser(100, rsdm.media_id);
  const rsd = await previewToUser(
    "odkSJ5-zNfHuaNBfTSN_bii4nFH4",
    rsdm.media_id
  );
  res.send([rsd]);
});
router.get("/createmenu", async function (req, res) {
  const info = await createMenu("odkSJ5-zNfHuaNBfTSN_bii4nFH4");
  res.send(info.data);
});
router.get("/power", function (req, res) {
  res.send("功耗测试");
});

router.get("/getTicket", async function (req, res) {
  const obj = await sign(req.query.url);
  res.send(obj);
  console.log(obj);
});

module.exports = router;
